//查找元素第一个位置和最后一个位置
public class leetcode34 {
    public int[] searchRange(int[] nums, int target) {
        int[] ret={-1,-1};
        if(nums.length==0){
            return ret;
        }

        //查找左端点
        int left=0;
        int right=nums.length-1;
        while(left<right){
            int mid=left+(right-left)/2;
            if(nums[mid]<target){
                left=mid+1;
            }else {
                right=mid;
            }
        }
        //判断相遇点
        if(nums[left]!=target){
            return ret;
        }
        ret[0]=left;
        //查找右端点
        int left2=0;
        int right2=nums.length-1;
        while(left2<right2){
            int mid=left2+(right2-left2+1)/2;
            if(nums[mid]<=target){
                left2=mid;
            }else{
                right2=mid-1;
            }
        }
        if(nums[left]!=target){
            return ret;
        }
        ret[1]=right2;
        return ret;
    }
}

